package com.tjmc.tiomsresource.service.impl;

import com.tjmc.tiomscore.dao.*;
import com.tjmc.tiomscore.model.*;
import com.tjmc.tiomsresource.parser.CMPrtParser;
import com.tjmc.tiomsresource.service.CMPrtService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * @author Luc
 * @version 1.0
 * @date 2020-05-20 15:41
 */
@Service("cmPrtService")
public class CMPrtServiceImpl extends CMSuperServiceImpl<TranPtnHwPrt> implements CMPrtService {

    @Autowired
    private CMPrtParser cmPrtParser;
    @Autowired
    private TranPtnHwPrtMapper tranPtnHwPrtMapper;
    @Autowired
    private TiPortMapper tiPortMapper;
    @Autowired
    private TiNelMapper tiNelMapper;
    @Autowired
    private TiCardMapper tiCardMapper;
    @Autowired
    private TiTplMapper tiTplMapper;
    @Autowired
    private TranPtnTplMapper tranPtnTplMapper;

    @Override
    public CMPrtParser getParser() {
        return cmPrtParser;
    }

    @Override
    public TranPtnHwPrtMapper getMapper() {
        return tranPtnHwPrtMapper;
    }

    @Override
    public boolean saveTranPtnHwDataToDB(List<File> xmlFiles) {
        return this.saveTranPtnHwDataToDB(xmlFiles, "PRT");
    }

    @Override
    public void buildTiPortToDB() {
        tiPortMapper.truncateTable();
        tiPortMapper.insertDataFromTranPtnPort();
    }

    @Override
    public void correctTplId() {
        tiPortMapper.correctTplId();
    }

    @Override
    public TiPort createPortFromTranPtnHwPrt(String rmuid) {
        if(StringUtils.isEmpty(rmuid)) {
            return null;
        }
        TranPtnHwPrtExample tranPtnHwPrtExample = new TranPtnHwPrtExample();
        tranPtnHwPrtExample.createCriteria().andRmuidEqualTo(rmuid);
        List<TranPtnHwPrt> tranPtnHwPrtList = tranPtnHwPrtMapper.selectByExample(tranPtnHwPrtExample);
        if(null == tranPtnHwPrtList || tranPtnHwPrtList.size() != 1) {
            return null;
        }
        TranPtnHwPrt tranPtnHwPrt = tranPtnHwPrtList.get(0);
        TiPort tiPort = new TiPort();
        tiPort.setNativename(tranPtnHwPrt.getNativename());
        tiPort.setRmuid(rmuid);
        tiPort.setCardId((long) 0);
        tiPort.setBusiId((long) 0);
        tiPort.setTplId((long) 0);
        tiPort.setTranPtnPortId((long) 0);

        TiNelExample tiNelExample = new TiNelExample();
        tiNelExample.createCriteria().andRmuidEqualTo(tranPtnHwPrt.getNermuid());
        List<TiNel> tiNelList = tiNelMapper.selectByExample(tiNelExample);
        if(null == tiNelList || tiNelList.size() != 1) {
            return null;
        }
        tiPort.setNelId(tiNelList.get(0).getId());

        TiCardExample tiCardExample = new TiCardExample();
        tiCardExample.createCriteria().andRmuidEqualTo(tranPtnHwPrt.getCardrmuid());
        List<TiCard> tiCardList = tiCardMapper.selectByExample(tiCardExample);
        if(tiCardList != null && tiCardList.size() == 1) {
            tiPort.setCardId(tiCardList.get(0).getId());
        }
        TranPtnTplExample tranPtnTplExample = new TranPtnTplExample();
        tranPtnTplExample.or().andAendportrmuidEqualTo(rmuid);
        tranPtnTplExample.or().andZendportrmuidEqualTo(rmuid);
        List<TranPtnTpl> tranPtnTplList = tranPtnTplMapper.selectByExample(tranPtnTplExample);
        if(null != tranPtnTplList && tranPtnTplList.size() > 0) {
            TiTplExample tiTplExample = new TiTplExample();
            tiTplExample.createCriteria().andRmuidEqualTo(tranPtnTplList.get(0).getRmuid());
            List<TiTpl> tiTplList = tiTplMapper.selectByExample(tiTplExample);
            if(null != tiTplList && tiTplList.size() > 0) {
                tiPort.setTplId(tiTplList.get(0).getId());
            }
        }

        tiPortMapper.insertSelective(tiPort);
        return tiPort;
    }

    @Override
    public List<TiPort> getTiPortsByTiNel(TiNel tiNel, int type) {
        if(null != tiNel) {
            TiPortExample tiPortExample = new TiPortExample();
            tiPortExample.createCriteria().andNelIdEqualTo(tiNel.getId());
            if(type == 1) {
                tiPortExample.createCriteria().andTplIdNotEqualTo((long) 0);
            }
            return tiPortMapper.selectByExample(tiPortExample);
        }
        return null;
    }

    @Override
    public TiPort getTiPortsByTiNelAndTiPrtNativename(TiNel tiNel, String nativename) {
        if(null != tiNel) {
            TiPortExample tiPortExample = new TiPortExample();
            tiPortExample.createCriteria().andNelIdEqualTo(tiNel.getId());
            tiPortExample.createCriteria().andNativenameEqualTo(nativename);
            List<TiPort> tiPortList = tiPortMapper.selectByExample(tiPortExample);
            if(null != tiPortList && tiPortList.size() > 0) {
                return tiPortList.get(0);
            }
        }
        return null;
    }
}
